/**
 * GWT Kad : Kademlia implementation for GWT
 * Copyright (C) 2012  Pierrick HYMBERT, Paul-André MISTRAL, Florian ALEXANDRE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.kad.gwt.web.client.kademelia.rpc.store;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;

import org.kad.gwt.web.client.kademelia.inject.KadModule;
import org.kad.gwt.web.client.kademelia.model.KadId;
import org.kad.gwt.web.client.kademelia.model.KadNode;
import org.kad.gwt.web.client.kademelia.model.rpc.store.KadRpcStoreRequest;
import org.kad.gwt.web.client.kademelia.rpc.AbstractRpcHandler;
import org.kad.gwt.web.client.kademelia.rpc.store.mvp.KadStoreEvent;
import org.kad.gwt.web.client.kademelia.transport.KadTransportService;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.storage.client.Storage;

/**
 * Handle the store rpc command.
 * 
 * @author Pierrick HYMBERT &lt;pierrick [dot] hymbert [at] gmail [dot] com&gt;
 */
@Singleton
public class StoreHandler extends AbstractRpcHandler implements KadStoreEvent.Handler {
    /**
     * The local storage service.
     */
    private final Storage localStorage;

    /**
     * Construct a new <code>StoreHandler</code>
     * 
     * @param bus
     *            The event bus
     * @param transport
     *            The kad transport service
     * @param localStorageIn
     *            The local storage provider
     */
    @Inject
    public StoreHandler(EventBus bus, KadTransportService transport, Storage localStorageIn, @Named(KadModule.KAD_LOCAL_NODE) Provider<KadNode> localNode) {
        super(transport, localNode);
        this.localStorage = localStorageIn;
        bus.addHandler(KadStoreEvent.getType(), this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onKadStore(final KadStoreEvent event) {
        KadRpcStoreRequest storeRequest = event.getStoreRequest();
        // Check that the item is not present in the local store
        if (localStorage.getItem(storeRequest.getKey().getValue()) == null) {
            localStorage.setItem(storeRequest.getKey().getValue(), storeRequest.getData());

            // Dispatch the store request to other nodes using the closest node
            KadNode closestNode = findClosestNode(storeRequest.getKey());
            getTransport().send(closestNode, new KadRpcStoreRequest(getLocalNode(), closestNode, storeRequest.getKey(), storeRequest.getData()));
        }
    }

    /**
     * @param key
     * @return
     */
    private KadNode findClosestNode(KadId key) {
        // TODO Auto-generated method stub
        return null;
    }
}
